<?php
class Users_model extends CI_Model{
    private $tbl = "users";
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }
    
    function login($username, $password) {
        $this->db->select('id_user', 'username', 'password');
        $this->db->from('users');
        $this->db->where('username', $username);
        $this->db->where('password', md5($password));
        $this->db->limit(1);
        
        $query = $this->db->get();
        if ($query->num_rows == 1) {
            
            return $query->result();
            
        } else {
            
            return false;
        }
    }

    function login_active($username, $password) {
        $this->db->select('level');
        $this->db->from('users');
        $this->db->where('username', $username);
        $this->db->where('password', md5($password));
         $this->db->where('level IS NULL');
        $this->db->limit(1);
        
        $query = $this->db->get();
        if ($query->num_rows == 1) {
            
            return true;
            
        } else {
            
            return false;
        }
    }
    
    public function add_user() {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
            'email' => $this->input->post('email_address'),
            'last_name' => $this->input->post('lastname'),
            'first_name' => $this->input->post('firstname'),
            'id_sex' => $this->input->post('sexual'),
        );
        
        $this->db->insert('users', $data);
    }
    
    public function active_user($username) {
        $this->db->select('level');
        $this->db->where('username',$username);
        $data = $this->db->get('users')->result_array();
        if($data == null)
            return 2;
        else if(!$data[0]['level']){
            $this->db->where('username',$username);
            $this->db->update('users', array('level' => 0));
            return 1;
        }else
            return 0;
    }
}